///index.js
//获取应用实例
const app = getApp()
var indexStart = 1;
const getToken = require('../../utils/get_token.js');
const getMemId = require('../../utils/memberId.js');
let memberId, openId;
let cookie, token;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    let tokens = await getToken.getToken();
    thou(that);
  }
}
const thou = (that) => {//token没有失效时执行的代码
  getAll(that);
  if (openId == '') {
    getToken.getOpenId(function (res) {
      wx.setStorageSync('openId', res.data.openId);
      if (memberId == '') {
        getMemId.getMemId(res.data.openId, function (res) {
          if (res.data.flag == 'true') {
            wx.setStorageSync('memberId', res.data.memberId);
          } else {
            wx.redirectTo({ url: '../login/login?id=4' })
          }
        })
      }
    });
  } else {
    if (memberId == '') {
      wx.redirectTo({ url: '../login/login?id=4' })
    }
  }
}

const getAll = function (that) {//订单类型
  let params = [ 'shop/pingTuan/huoPinDdAndSqDdCount', {"memberId":memberId,"tuanIncome":0}]
  getToken.getData(function (res) {
    console.log(res.data)
    let data = res.data;
    data.allCount = Number(data.allCount) <= 99 ? Number(data.allCount) : 99;;
    data.needPin = Number(data.needPin) <= 99 ? Number(data.needPin) : 99;
    data.waitShip = Number(data.waitShip) <= 99 ? Number(data.waitShip) : 99;
    data.hadShip = Number(data.hadShip) <= 99 ? Number(data.hadShip) : 99;
    data.failPin = Number(data.failPin) <= 99 ? Number(data.failPin) : 99;
    that.setData({
      oList: data
    })
  }, ...params)
};
Page({
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    userInfo: {},
    boolT:true,
    nickName:'',
    hidden: false,
    tcShow: false,
    oList: { 'allCount': 0, 'needPin': 0, 'waitShip': 0, 'hadShip': 0, 'failPin': 0},
    mId:''
  },
  onLoad: function () {
    let that = this;
    memberId = wx.getStorageSync('memberId') || '';
    openId = wx.getStorageSync('openId') || '';
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
    indexStart = 1;
    if (memberId == '') {
        wx.redirectTo({ url: '../login/login?id=4' })
    }
    wx.getUserInfo({
      success: function(res) {
        var userInfo = res.userInfo
        that.data.nickName = userInfo.nickName
      }
    })
  },
  onShow: function() {// 页面出现在前台时执行
    let that = this;
    if(memberId != ''){
      ckToken(that); 
    }
  },
	toAddress:function(e){//跳转到地址页面
    let that = this;
		if (that.data.boolT) {
			that.data.boolT = false;
			wx.navigateTo({
				url: '../m_address/m_address',
				success: function () {
					setTimeout(function () {
						that.data.boolT = true;
					}, 500)
				}
			})
		}
	},
  toRecord: function (e) {//跳转到记录页面
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../browse/browse',
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
  toProblem: function (e) {//跳转到常见页面
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../problem/problem',
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
  toContact: function (e) {//跳转到联系客户页面../contact/contact
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../contact/contact',
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
  goColelction: function (e) {//跳转到收藏页面
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../collection/collection',
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
	enter: function (e) {//订单跳转
    let currentTab = e.currentTarget.dataset.currenttab;
    let that = this;
		if (that.data.boolT) {
			that.data.boolT = false;
			wx.navigateTo({
        url: '../p_order/p_order?currentTab=' + currentTab,
				success: function () {
					setTimeout(function () {
						that.data.boolT = true;
					}, 500)
				}
			})
		}
	},
  go_order: function (e) {//订单跳转
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../order/order',
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
  preImage:function(e){//保存图片
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.getImageInfo({
        src: '../../image/04_01.jpg',
        success: function (res) {
          let path = res.path;
          setTimeout(function () { that.data.boolT = true; }, 500)
          wx.getSetting({
            success(res) {
              wx.authorize({
                scope: 'scope.writePhotosAlbum',
                success(res) {
                  wx.saveImageToPhotosAlbum({
                    filePath: path,
                    success(result) {
                      wx.showToast({
                        title: '已保存到系统相册',
                        icon: 'success',
                        duration: 2000
                      })
                    }
                  })
                }
              })

            }
          })
        }
      })
      
    }
  },
	onShareAppMessage: function (res) {
   let that=this;
   let ctype=encodeURIComponent(app.globalData.model)
    if(res.from === 'button'){
      let params = ['common/mtc/gmtr', {"A":memberId,"B":'8a22d0135c7c6a72015c7c9f021e009d',"C":ctype,"D":''}]
        getToken.getData(function (res) {
          console.log(res.data)              
        }, ...params)
      return {
        title: `您的好友${that.data.nickName}邀你一起体验不一样的拼团(社邻拼),一起拼更省钱`,
        path: '/pages/index/index?shareId=' + memberId,
        imageUrl:'http://image.globalhexi.com/oss/img/image/20201231/source/f56124c305b742e79ea90b9c4222b6a4.jpg',
        success: function (res) {},
      }
    }
	}
})
